﻿using System;
using System.Web;
using System.Net.Mail;
using System.Configuration;

using GG.Data;

namespace GG.Mail
{
    public class MyMailManager
    {
        #region Private Variable
        private MailMessage mail = null;
        private MailAddress mailAddress = null;
        private SmtpClient mailClient = null;
        private int clientPort = -1;
        private string clientHost = String.Empty;
        private bool isEnabledSsl = false;
        private int mailType = -1;
        private string mailBody = "";
        private MailPriority mailPriority = MailPriority.Normal;

        static string smtphost = System.Configuration.ConfigurationSettings.AppSettings["smtphost"].ToString();// "smtp.gmail.com";
        static string smtpport = System.Configuration.ConfigurationSettings.AppSettings["smtpport"].ToString();//"465";
        static string smtpaccount = System.Configuration.ConfigurationSettings.AppSettings["smtpaccount"].ToString();//"comparexpress.submissions@gmail.com";
        static string smtpauthcode = System.Configuration.ConfigurationSettings.AppSettings["smtpauthcode"].ToString();//"sg2010express";
        static bool smtpenablessl = true;

        #endregion

        public MyMailManager()
        {
            mailClient = new SmtpClient();
            mailClient.Host = "smtp.gmail.com";
            mailClient.Port = 465;
            mailClient.EnableSsl = true;
        }

        public MyMailManager(string HostName, string Port, bool EnableSSL, string Account, string AuthCode)
        {
            smtphost = HostName;
            smtpenablessl = EnableSSL;
            smtpport = Port;
            smtpaccount = Account;
            smtpauthcode = AuthCode;
        }

        public static bool SendEmailByAttachedment(string EmailTitle, string mailBody, string ToEmailAddress, string CCEmailAddress, string BCCEmailAddress, string[] fileList)
        {
            //string mailBody = "Dear CX Team, <br />Please check comparexpress.com is up or down now. <br/>From here, can't be requested to "+ URL +".<br /> This message was sent by "+ Location +" - CX Monitoring program. ";


            bool _result = SendMailNewByAttachments(smtphost,
                        465,
                        smtpaccount,
                        smtpauthcode,
                        EmailTitle,
                        smtpaccount,
                        EmailTitle,
                        ToEmailAddress,
                        CCEmailAddress,
                        BCCEmailAddress,
                        EmailTitle,
                        mailBody,
                        smtpenablessl, fileList);
            return _result;
            //return true;
        }

        public static bool SendMailNew(string sHost, int nPort, string sUserName, string sPassword, string sFromName, string sFromEmail,
        string sToName, string sToEmail, string sCCEmail, string sBCCEmail, string sHeader, string sMessage, bool fSSL)
        {
            if (sToName.Length == 0)
                sToName = sToEmail;
            if (sFromName.Length == 0)
                sFromName = sFromEmail;

            System.Web.Mail.MailMessage Mail = new System.Web.Mail.MailMessage();
            Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"] = sHost;
            Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"] = 2;

            Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"] = nPort.ToString();
            if (fSSL)
                Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"] = "true";

            if (sUserName.Length == 0)
            {
                //Ingen auth 
            }
            else
            {
                Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1;
                Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = sUserName;
                Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = sPassword;
            }

            Mail.To = sToEmail;
            Mail.Cc = sCCEmail;
            Mail.Bcc = sBCCEmail;
            Mail.From = sFromEmail;
            Mail.Subject = sHeader;
            Mail.Body = sMessage;
            Mail.BodyFormat = System.Web.Mail.MailFormat.Html;
            try
            {
                System.Web.Mail.SmtpMail.SmtpServer = sHost;
                System.Web.Mail.SmtpMail.Send(Mail);
            }
            catch
            {
                return false;
            }
            finally
            {
                if (ConfigurationManager.AppSettings["MailCountActive"].ToString() == "1")
                {
                    MyData _myData = new MyData();
                    _myData.InsertMailCountLog("GG Service - CI Renewal Mail");
                }
            }
            return true;
        }

        public static bool SendMailNewByAttachments(string sHost, int nPort, string sUserName, string sPassword, string sFromName, string sFromEmail,
        string sToName, string sToEmail, string sToCCEmail, string sToBCCEmail, string sHeader, string sMessage, bool fSSL, string[] fileList)
        {
            if (sToName.Length == 0)
                sToName = sToEmail;
            if (sFromName.Length == 0)
                sFromName = sFromEmail;

            System.Web.Mail.MailMessage Mail = new System.Web.Mail.MailMessage();
            Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"] = sHost;
            Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"] = 2;

            Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"] = nPort.ToString();
            if (fSSL)
                Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"] = "true";

            if (sUserName.Length == 0)
            {
                //Ingen auth 
            }
            else
            {
                Mail.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1;
                Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = sUserName;
                Mail.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = sPassword;
            }

            Mail.To = sToEmail;
            Mail.From = sFromEmail;
            //Mail.Subject = sHeader;
            Mail.Body = sMessage;
            Mail.Cc = sToCCEmail;
            Mail.Bcc = sToBCCEmail;
            Mail.BodyEncoding = System.Text.Encoding.UTF8;
            Mail.Subject = "=?UTF-8?B?" + Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(sHeader)) + "?=";

            for (int i = 0; i < fileList.Length; i++)
            {
                if (fileList[i] != string.Empty)
                {
                    Mail.Attachments.Add(new System.Web.Mail.MailAttachment(fileList[i].ToString()));
                }
            }

            Mail.BodyFormat = System.Web.Mail.MailFormat.Html;
            try
            {
                System.Web.Mail.SmtpMail.SmtpServer = sHost;
                System.Web.Mail.SmtpMail.Send(Mail);
            }
            catch (Exception Ex)
            {
                Ex.ToString();
                return false;
            }
            finally
            {
                if (ConfigurationManager.AppSettings["MailCountActive"].ToString() == "1")
                {
                    MyData _myData = new MyData();
                    _myData.InsertMailCountLog("GG Service - CI Renewal Mail");
                }
            }
            return true;
        }

        public static DateTime GetUTCDateTime()
        {
            DateTime _CurrentDateTime = DateTime.UtcNow.AddHours(7);
            return _CurrentDateTime;
        }

    }
}
